// pages/backCoupon/backCoupon.js
const http = require('../../network/http')
const api = require('../../network/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeId: '',
    detailId: '',
    couponList: [],
    couponId: '',
    pageNumber: 1,
    pages: 0,
  },
    /**
   * 获取优惠券列表
   */
  getGiveList() {
    http.request({
      showLoading: true,
      url: api.getGiveList,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        pageNumber: this.data.pageNumber,
        pageSize: 20,
      },
    }).then(res => {
      if (res.data.code == 200) {
        console.log("优惠券表数据返回:");
        console.log(res.data.result);
        var newList = this.data.couponList;
        if(res.data.result.records != null && res.data.result.records.length > 0){
          for(var i=0; i < res.data.result.records.length; i++){
            newList.push(res.data.result.records[i]);
          }
        }
        this.setData({
          'couponList': newList,
          pages: res.data.result.pages,
          // totalPrice: res.data.result[0].monthTotalPrice,
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });

  }, 
  clickItem: function(e){
    var $data = e.currentTarget.dataset;
    console.log($data.bean);
    this.setData({
      couponId: $data.bean.id,
    })
  }, 
  confirmChoose: function(e){
    http.request({
      showLoading: true,
      shake: true,
      url: api.cancelTrade,
      method: "POST",
      data: {
        cancelMode: '3',
        tradeId: this.data.detailId,
        couponId: this.data.couponId
      },
    }).then(res => {
      if (res.data.code == 200) {
        // this.hiddenCancel();
        setTimeout(function () {
          wx.showToast({
            title: '订单取消成功',
            icon: "none",
            duration: 2000
          });
        }, 500);
        wx.navigateBack({
          delta: 2,
        })
      }else{
        setTimeout(function () {
          wx.showToast({
            title: '订单取消失败，请稍后再试',
            icon: "none",
            duration: 2000
          });
        }, 500);
      }
    }).catch(err => {
      console.log(err)
    });
  },
  closeview: function(e){
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    console.log(options);
    that.setData({
      storeId: options.storeId,
      detailId: options.detailId,
      pageNumber: 1,
      pages: 0,
      'couponList': [],
    })
    this.getGiveList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNumber: 1,
      pages: 0,
      'couponList': [],
    })
    this.getGiveList();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 500);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.pages > this.data.pageNumber){
      var newPage = this.data.pageNumber + 1;
      this.setData({
        pageNumber: newPage,
        pages: 0,
      })
      this.getGiveList();
    }else{
      wx.showToast({
        title: '暂无更多数据',
        icon: "none",
        duration: 2000
      });
    }
    wx.pageScrollTo({
      // scrollTop: 0,
      duration: 300,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})